package com.fita.webpay.entity.context;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import com.demo2do.core.utils.DateFormatUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fita.webpay.entity.PaymentContext;
import com.fita.webpay.entity.config.HandpayConfig;

/**
 * 
 * @author mohanlan
 *
 */
public class HandpayContext {
    
    private String insCode;
    
    private String insMerchantCode;
    
    private String hpMerCode;
    
    private String orderNo;
    
    private String orderTime;
    
    private String currencyCode;
    
    private String orderAmount;
    
    private String name;
    
    private String idNumber;
    
    private String accNo;
    
    private String telNo;
    
    private String productType;
    
    private String paymentType;
    
    private String nonceStr;
    
    private String backUrl;
    
    private String signature;
    
    /**
     * The default constructor
     */
    public HandpayContext() {
    }
    
    /**
     * 
     * @param handpayConfig
     */
    public HandpayContext(HandpayConfig handpayConfig) {
        this.insCode = handpayConfig.getAppid();
        this.insMerchantCode = handpayConfig.getAppNum();
        this.hpMerCode = handpayConfig.getSellerId();
        this.currencyCode = handpayConfig.getCurrencyCode();
        this.productType = handpayConfig.getProductType();
        this.paymentType = handpayConfig.getPaymentType();
        this.backUrl = handpayConfig.getNotifyUrl();
        this.nonceStr = RandomStringUtils.randomAlphabetic(32);
    }
    
    /**
     * 
     * @param paymentContext
     * @return
     */
    public HandpayContext init(PaymentContext paymentContext) {
        this.orderNo = paymentContext.getOutTradeNo();
        this.orderTime = DateFormatUtils.format(paymentContext.getCreateTime(), "yyyyMMddHHmmss");
        this.orderAmount = paymentContext.getAmountAsString();
        
        return this;
    }
    
    /**
     * Check whether this {@link WechatpayContext} is valid
     * @return
     */
    @JsonIgnore
    public boolean isValid() {
        return StringUtils.isNotBlank(this.insCode) &&
                StringUtils.isNoneBlank(this.insMerchantCode) &&
                StringUtils.isNoneBlank(this.hpMerCode) &&
                StringUtils.isNoneBlank(this.orderNo) &&
                StringUtils.isNoneBlank(this.orderAmount) &&
                StringUtils.isNoneBlank(this.backUrl);
    }
    
    /**
     * 
     * @return
     */
    @JsonIgnore
    public String generateSignString() {
        //insCode|insMerchantCode|hpMerCode|orderNo|orderTime|orderAmount|name|idNumber|accNo| telNo|productType|paymentType| nonceStr|signKey
        return com.demo2do.core.utils.StringUtils.connect(this.insCode, "|", this.insMerchantCode, "|", this.hpMerCode, "|",
                                                          this.orderNo, "|", this.orderTime, "|", this.orderAmount, "|", this.name , "|",
                                                          this.idNumber, "|", this.accNo, "|", this.telNo, "|", this.productType, "|",
                                                          this.paymentType, "|", this.nonceStr);
    }
    
    /**
     * Make signature
     * @param sign
     * @return
     */
    public HandpayContext sign(String sign) {
        this.signature = sign;
        return this;
    }

    /**
     * @return the insCode
     */
    public String getInsCode() {
        return insCode;
    }

    /**
     * @return the insMerchantCode
     */
    public String getInsMerchantCode() {
        return insMerchantCode;
    }

    /**
     * @return the hpMerCode
     */
    public String getHpMerCode() {
        return hpMerCode;
    }

    /**
     * @return the orderNo
     */
    public String getOrderNo() {
        return orderNo;
    }

    /**
     * @return the orderTime
     */
    public String getOrderTime() {
        return orderTime;
    }

    /**
     * @return the currencyCode
     */
    public String getCurrencyCode() {
        return currencyCode;
    }

    /**
     * @return the orderAmount
     */
    public String getOrderAmount() {
        return orderAmount;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the idNumber
     */
    public String getIdNumber() {
        return idNumber;
    }

    /**
     * @return the accNo
     */
    public String getAccNo() {
        return accNo;
    }

    /**
     * @return the telNo
     */
    public String getTelNo() {
        return telNo;
    }

    /**
     * @return the productType
     */
    public String getProductType() {
        return productType;
    }

    /**
     * @return the paymentType
     */
    public String getPaymentType() {
        return paymentType;
    }

    /**
     * @return the nonceStr
     */
    public String getNonceStr() {
        return nonceStr;
    }
    /**
     * @return the backUrl
     */
    public String getBackUrl() {
        return backUrl;
    }

    /**
     * @return the signature
     */
    public String getSignature() {
        return signature;
    }

    /**
     * @param insCode the insCode to set
     */
    public void setInsCode(String insCode) {
        this.insCode = insCode;
    }

    /**
     * @param insMerchantCode the insMerchantCode to set
     */
    public void setInsMerchantCode(String insMerchantCode) {
        this.insMerchantCode = insMerchantCode;
    }

    /**
     * @param hpMerCode the hpMerCode to set
     */
    public void setHpMerCode(String hpMerCode) {
        this.hpMerCode = hpMerCode;
    }

    /**
     * @param orderNo the orderNo to set
     */
    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    /**
     * @param orderTime the orderTime to set
     */
    public void setOrderTime(String orderTime) {
        this.orderTime = orderTime;
    }

    /**
     * @param currencyCode the currencyCode to set
     */
    public void setCurrencyCode(String currencyCode) {
        this.currencyCode = currencyCode;
    }

    /**
     * @param orderAmount the orderAmount to set
     */
    public void setOrderAmount(String orderAmount) {
        this.orderAmount = orderAmount;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param idNumber the idNumber to set
     */
    public void setIdNumber(String idNumber) {
        this.idNumber = idNumber;
    }

    /**
     * @param accNo the accNo to set
     */
    public void setAccNo(String accNo) {
        this.accNo = accNo;
    }

    /**
     * @param telNo the telNo to set
     */
    public void setTelNo(String telNo) {
        this.telNo = telNo;
    }

    /**
     * @param productType the productType to set
     */
    public void setProductType(String productType) {
        this.productType = productType;
    }

    /**
     * @param paymentType the paymentType to set
     */
    public void setPaymentType(String paymentType) {
        this.paymentType = paymentType;
    }

    /**
     * @param nonceStr the nonceStr to set
     */
    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    /**
     * @param backUrl the backUrl to set
     */
    public void setBackUrl(String backUrl) {
        this.backUrl = backUrl;
    }

    /**
     * @param signature the signature to set
     */
    public void setSignature(String signature) {
        this.signature = signature;
    }
}
